﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Main.Packets
{
    public class FreeConnectionPacket : Packet
    {
        private byte adresseSource;
        private byte adresseDestination;
        private Raisons raison;

        public FreeConnectionPacket(byte numeroConnexion, PacketType type, byte adresseSource, byte adresseDestination, Raisons raison)
            : base(numeroConnexion, type)
        {
            this.adresseSource = adresseSource;
            this.adresseDestination = adresseDestination;
            this.raison = raison;
        }

        [JsonProperty(Order = 10)]
        public byte AdresseSource
        {
            get { return adresseSource; }
            set { adresseSource = value; }
        }
        [JsonProperty(Order = 20)]
        public byte AdresseDestination
        {
            get { return adresseDestination; }
            set { adresseDestination = value; }
        }

        [JsonProperty(Order = 30)]
        public Raisons Raison
        {
            get { return raison; }
            set { raison = value; }
        }

        public static FreeConnectionPacket CreateFreeConnectionIndicationPacket(byte numeroConnexion, byte adresseSource, byte adresseDestination, Raisons raison)
        {
            FreeConnectionPacket freeConnectionPacket = new FreeConnectionPacket(numeroConnexion, PacketType.CreateTypeForFreeConnectionPacket(), adresseSource, adresseDestination, raison);
            return freeConnectionPacket;
        }

        public static FreeConnectionPacket CreateFreeConnectionRequestPacket(byte numeroConnexion, byte adresseSource, byte adresseDestination)
        {
            FreeConnectionPacket freeConnectionPacket = new FreeConnectionPacket(numeroConnexion, PacketType.CreateTypeForFreeConnectionPacket(), adresseSource, adresseDestination, Raisons.None);
            return freeConnectionPacket;
        }

        public static FreeConnectionPacket CreateFreeConnectionPacket(string str)
        {
            return JsonConvert.DeserializeObject<FreeConnectionPacket>(str) as FreeConnectionPacket;
        }

        public override void WriteJson(System.IO.StreamWriter sw)
        {
            sw.WriteLine(JsonConvert.SerializeObject(this));
        }
    }
}
